define(function (require,exports,module){
	/**--tmpl-require--**/
    require("ajaxproxy");
	var template = require('jtemplate');
	require('jdialog');
	var common = require('common');

	exports.init = function(config){

		/**
		 * 确认订单
		 */
		$(".confirmation").click(function(){
			var id = $(this).attr('data-id');
			layer.confirm('是否确定订单信息无误',['确定','取消'],function(){
				layer.load();
				$.post(config.merchantConfirmationUrl,{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('操作成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});

		/**
		 * 取消订单
		 */
		$(".cancelOrder").click(function(){
			var id = $(this).attr('data-id');
			layer.confirm('是否确定取消订单',['确定','取消'],function(){
				layer.load();
				$.post(config.cancelOrderUrl,{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('操作成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});

		/**
		 * 发货--买家自提
		 */
		$(".delivery").click(function(){
			var id = $(this).attr('data-id');
			layer.confirm('是否确定发货？',['确定','取消'],function(){
				layer.load();
				$.post(config.addShippingUrl,{id:id},function(data){
					layer.closeAll('loading');
					if(data.code == 0 || data.code=='success'){
						layer.msg('操作成功');
						document.location.reload();
					}else{
						layer.msg(data.message);
					}
				},'json');
			});
		});

		/**
		 * 流水记录
		 */
		$('.select_record').on('click', function(){
			var member_id = $("input[name='member_id']").val();
			var order_id = $(this).attr('data-id');
			layer.open({
				type: 2,
				title: '确认银行流水',
				shadeClose: false,
				shade: 0.8,
				btn:["确定收款","取消"],
				area: ['90%', '90%'],
				content: config.billRecordsUrl+'?id='+member_id,
				yes:function (index,layero) {
					var body = layer.getChildFrame('body');
					var ids=[];
					body.find("#J_ListTable input[name='record_id[]']:checked").each(function(i,e){
						ids.push(e.value);
					});

					if(ids.length>0){
						ids = ids.join(",");
						$.post(config.confirmOrderPayUrl,{order_id:order_id,id:ids},function(data){
							layer.closeAll();
							if(data.code == 0 || data.code=='success'){
								layer.msg('确认收款成功');
								parent.document.location.reload();
							}else{
								layer.msg(data.message);
							}
						},'json');
					}

				}
			});
		});


        /**
         * 发货 配送信息
         */
        $('.popwin').on('click', function() {

            var templateId = $(this).data("template");
			var id = $(this).data('id');
            var options = {
                title : "配送单",
                content : template(templateId, {id:id}),
                icon : 'none',
                height : 550,
                button : {
                    '保存' : function() {
                        var formId = 'content-add-form';
                        if ( !common.formCheckHandler(formId, false) ) return false;
                        //collect form data
                        var formData = $('#' + formId).serialize();
                        popWin.lock();
                        $.post(config.addShippingUrl, formData, function (result) {
                            common.ajaxCallback(result, function() {
                                //popWin.unlock();
                                popWin.close();
								parent.document.location.reload(true);
                            });
                        }, 'json');

                    },
                    '取消' : function() {
                        popWin.close();
                    }
                }

            }
            var options = $.extend(__global.jdialog, options);
            var popWin = JDialog.win.work(options);

            common.initElements(); //初始化元素组件

        });
	};

});
